﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;


namespace QRCode
{
	public class DataEntity
	{
		public string DNNo
		{
			get;
			set;
		}

		public string PONo
		{
			get;
			set;
		}

		public string PartNum
		{
			get;
			set;
		}

		public decimal Qty
		{
			get;
			set;
		}

		public decimal Proc
		{
			get;
			set;
		}

		public int Total
		{
			get;
			set;
		}

		public decimal Remainder
		{
			get;
			set;
		}

		public string UOM
		{
			get;
			set;
		}

		public string VendorName
		{
			get;
			set;
		}

		public string Date
		{
			get;
			set;
		}

		public byte[] QRCode
		{
			get;
			set;
		}

		public string Lot
		{
			get;
			set;
		}

		public string VendorID
		{
			get;
			set;
		}

		public string OurLot
		{
			get;
			set;
		}

		public string IsBackUp
		{
			get;
			set;
		}

		public string Comments
		{
			get;
			set;
		}

		// QRCode.DataEntity
		public static DataTable EntityToDataTable(DataEntity[] entity)
		{
			DataTable dataTable = new DataTable();
			DataTable result;
			if (entity == null || entity.Length <= 0)
			{
				result = null;
			}
			else
			{
				DataEntity dataEntity = new DataEntity();
				PropertyInfo[] properties = dataEntity.GetType().GetProperties();
				for (int i = 0; i < properties.Length; i++)
				{
					PropertyInfo propertyInfo = properties[i];
					dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
				}
				string path = Application.StartupPath + "\\log.txt";
				if (!File.Exists(path))
				{
					File.Create(path).Close();
				}
				List<string> list = new List<string>();
				StreamReader streamReader = new StreamReader(path, Encoding.Default);
				string text;
				while ((text = streamReader.ReadLine()) != null)
				{
					if (!string.IsNullOrEmpty(text))
					{
						list.Add(text);
					}
				}
				streamReader.Dispose();
				for (int i = 0; i < entity.Length; i++)
				{
					DataEntity dataEntity2 = entity[i];
					for (int j = 0; j < dataEntity2.Total; j++)
					{
						DataRow dataRow = dataTable.NewRow();
						string text2 = dataEntity2.VendorID + "-" + DateTime.Now.ToString("yyyyMMdd");
						text2 = text2;
						list.Add(text2);
						dataEntity2.OurLot = text2;
						properties = dataEntity2.GetType().GetProperties();
						for (int k = 0; k < properties.Length; k++)
						{
							PropertyInfo propertyInfo = properties[k];
							dataRow[propertyInfo.Name] = propertyInfo.GetValue(dataEntity2, null);
						}
						dataRow["Qty"] = dataEntity2.Proc;
						if (dataEntity2.Remainder > 0m && j == dataEntity2.Total - 1)
						{
							dataRow["Qty"] = dataEntity2.Remainder;
						}
						if (dataEntity2.Proc > dataEntity2.Qty)
						{
							dataRow["Qty"] = dataEntity2.Qty;
						}
						QRCodeHandler qRCodeHandler = new QRCodeHandler();
						string str = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/QRCode/";
						string qRString = string.Concat(new object[]
						{
					"ID:",
					j,
					"|PO:",
					dataEntity2.PONo,
					"|PartNum:",
					dataEntity2.PartNum,
					"|Qty:",
					dataRow["Qty"],
					"|UOM:",
					dataEntity2.UOM,
					"|Date:",
					dataEntity2.Date,
					"|PackSlip:",
					dataEntity2.DNNo,
					"|VendorName:",
					dataEntity2.VendorName,
					"|Lot:",
					dataEntity2.Lot,
					"|VendorID:",
					dataEntity2.VendorID,
					"|OurLot:",
					dataEntity2.OurLot,
					"|IsBackUp:",
					dataEntity2.IsBackUp,
					"|Code:",
					Guid.NewGuid()
						});
						string logoFilePath = str + "log.JPG";
						string text3 = str + "myCode.jpg";
						qRCodeHandler.CreateQRCode(qRString, "Byte", 5, 0, "H", text3, true, logoFilePath);
						dataRow["QRCode"] = DataEntity.ImageToByte(text3);
						dataTable.Rows.Add(dataRow);
					}
				}
				StreamWriter streamWriter = new StreamWriter(path, false);
				for (int j = 0; j < list.Count; j++)
				{
					streamWriter.WriteLine(list[j]);
				}
				streamWriter.Dispose();
				result = dataTable;
			}
			return result;
		}


		public static byte[] ImageToByte(string path)
		{
			byte[] array = null;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (Bitmap bitmap = new Bitmap(path))
				{
					bitmap.Save(memoryStream, ImageFormat.Bmp);
					memoryStream.Position = 0L;
					array = new byte[memoryStream.Length];
					memoryStream.Read(array, 0, Convert.ToInt32(memoryStream.Length));
					memoryStream.Flush();
				}
			}
			return array;
		}

		public static DataEntity[] DataTableToEntity(DataTable dt)
		{
			List<DataEntity> list = new List<DataEntity>();
			DataEntity[] result;
			if (dt == null || dt.Rows.Count <= 0)
			{
				result = null;
			}
			else
			{
				IEnumerator enumerator = dt.Rows.GetEnumerator();
				{
					DataRow item;
					while (enumerator.MoveNext())
					{
						item = (DataRow)enumerator.Current;
						if (!string.IsNullOrEmpty(item[0].ToString()))
						{
							DataEntity dataEntity = list.FirstOrDefault((DataEntity x) => x.PONo == item[0].ToString() && x.PartNum == item[1].ToString() && x.UOM == item[3].ToString());
							decimal qty = 0m;
							DateTime now = DateTime.Now;
							if (item[2] == null || !decimal.TryParse(item[2].ToString(), out qty))
							{
								throw new Exception(string.Concat(new string[]
								{
									"[",
									item[0].ToString(),
									"] [",
									item[1].ToString(),
									"] 数量错误."
								}));
							}
							dataEntity = new DataEntity();
							dataEntity.DNNo = item[4].ToString();
							dataEntity.PONo = item[0].ToString();
							dataEntity.PartNum = item[1].ToString();
							dataEntity.Qty = qty;
							dataEntity.UOM = item[3].ToString();
							dataEntity.VendorID = item[5].ToString();
							dataEntity.VendorName = item[6].ToString();
							dataEntity.Lot = item[8].ToString();
							dataEntity.OurLot = dataEntity.VendorID + "-" + DateTime.Now.ToString("yyyyMMdd");
							dataEntity.IsBackUp = item[9].ToString();
							dataEntity.Comments = item[10].ToString();
							if (item[7] != null && !DateTime.TryParse(item[7].ToString(), out now))
							{
								now = DateTime.Now;
							}
							dataEntity.Date = now.ToString("yyyy-MM-dd");
							list.Add(dataEntity);
						}
					}
				}
				result = list.ToArray();
			}
			return result;
		}
	}
}

